/*
 * @(#)XfeditSelectKbType.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfedit;

//import xfuzzy.lang.*;
import xfuzzy.util.*;
import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
//import java.util.EventObject;

/**
 * Di�logo para seleccionar el tipo de base de conocimiento a crear en el editor
 * 
 * @author Pablo Martin Mu�oz
 * 
 */
public class XfeditSelectKbType extends JDialog
implements ActionListener, MouseListener, ListSelectionListener, KeyListener {

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603020L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Lista de bases de conocimiento disponibles
	 */
	private XList kblist;
	
	/**
	 * Tipo de base de conocimiento seleccionada
	 */
	private String knowledgebase;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfeditSelectKbType(Xfedit xfedit) {
		super(xfedit,"Xfedit", true);

		String lb[] = {"Create","Cancel"};
		XCommandForm form = new XCommandForm(lb,lb,this);
		form.setCommandWidth(120);
		form.block();

		kblist = new XList("Available KnowledgeBases");
		String avKb[] = {"RuleSet","RuleList"};
		kblist.setPreferredWidth(100);
		kblist.addMouseListener(this);
		kblist.addListSelectionListener(this);
		kblist.addKeyListener(this);
		kblist.setListData(avKb);

		Box box = new Box(BoxLayout.X_AXIS);
		box.add(Box.createHorizontalStrut(5));
		box.add(kblist);
		box.add(Box.createHorizontalStrut(5));

		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(Box.createVerticalStrut(5));
		content.add(box);
		content.add(Box.createVerticalStrut(5));
		content.add(form);
		setFont(XConstants.font);
		setLocationRelativeTo(xfedit);
		pack();
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Devuelve el objeto seleccionado
	 */
	public Object getSelection() {
		if(knowledgebase != null) return knowledgebase;
		return null;
	}

	/**
	 * Interfaz MouseListener. Acci�n al pulsar el rat�n
	 */
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount() == 2) { if(set()) setVisible(false); }
	}
	
	/**
	 * Interfaz MouseListener. Acci�n al entrar en la ventana
	 */	
	public void mouseEntered(MouseEvent e) {
	}
	
	/**
	 * Interfaz MouseListener. Acci�n al salir de la ventana
	 */
	public void mouseExited(MouseEvent e) {
	}
	
	/**
	 * Interfaz MouseListener. Acci�n al apretar un bot�n del rat�n
	 */
	public void mousePressed(MouseEvent e) {
	}

	/**
	 * Interfaz MouseListener. Acci�n al soltar un bot�n del rat�n
	 */
	public void mouseReleased(MouseEvent e) {
	}

	/**
	 * Interfaz ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("Create")) { if(set()) setVisible(false); }
		else if(command.equals("Cancel")) setVisible(false);
	}

	/**
	 * Interfaz ListSelectionListener
	 */
	/*public void valueChanged(ListSelectionEvent e) {
		int list = getEventModule(e);
		listSelection(list);
	}*/

	/**
	 * Interfaz KeyListener. Acci�n al soltar una tecla
	 */
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == 226) {
			e.consume();
			if(set()) setVisible(false);
		}
	}
	
	/**
	 * Interfaz KeyListener. Acci�n al apretar una tecla
	 */
	public void keyPressed(KeyEvent e) {
	}
	
	/**
	 * Interfaz KeyListener. Acci�n al teclear.
	 */
	public void keyTyped(KeyEvent e) {
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Elimina la selecci�n de una lista al activar otra
	 */
	/*private void listSelection(int list) {
		if(kblist.isSelectionEmpty()) return;
		else kblist.clearSelection();
	}*/

	/**
	 * Obtiene el c�digo de la lista que genera el evento
	 */
	/*private int getEventModule(EventObject e) {
		int kind = -1;
		if(e.getSource() == rblist.getList() ) kind = RULEBASE;
		if(e.getSource() == cblist.getList() ) kind = CRISPBLOCK;
		return kind;
	}*/

	/**
	 * Verifica que se haya seleccionado un m�dulo
	 */
	private boolean set() {
		if(!kblist.isSelectionEmpty()) {
			knowledgebase = (String) kblist.getSelectedValue();
		}
		return (knowledgebase != null);
	}

	public void valueChanged(ListSelectionEvent e) {
		
	}
}
